// packageA/pages/articleDetail.js
var WxParse = require('../wxParse/wxParse.js');
let videoAd = null
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type:'',
        article:{},
        apiList:[],
        isShowYanchang:false,
        keyValue:''
    },
    yanchangKey(){
      wx.showLoading({
        title: '请稍等...',
        mask:true
      })
      let that = this
      wx.request({
          url: 'https://guanhe.work/xiaochengxu/yanchangKey',
          data:{
              keyValue:that.data.keyValue
          },
          success(res){
            wx.hideLoading({
              success: (res) => {},
            })
              if(res.data.code == 200){
                wx.showToast({
                  title: '延长成功',
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon:'none'
                })
              }
                
                  
                
          },
          fail(e){
              wx.hideLoading({
                success: (res) => {},
              })
          }
      })
    },
    changeInp(e){
      this.setData({keyValue:e.detail.value})
    },
    showYanchang(){
      let that = this
      this.setData({isShowYanchang:!that.data.isShowYanchang,keyValue:''})
      console.log(that.data.isShowYanchang)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        if (wx.createRewardedVideoAd) {
            videoAd = wx.createRewardedVideoAd({
              adUnitId: 'adunit-ce63d91002f85834'
            })
            videoAd.onLoad(() => { })
            videoAd.onError((err) => { })
            videoAd.onClose((status) => { 
              if (status && status.isEnded || status === undefined) {
                // 正常播放结束，下发奖励
                // continue you code
                wx.showLoading({
                  title: '请稍等...',
                })
                if(that.data.e.currentTarget.dataset.keyname){
                    that.getPermission(that.data.e)
                } else if(that.data.e.currentTarget.dataset.gitaddress){
                    that.getGitAddress(that.data.e)
                } else {
                  that.yanchangKey()
                }
                console.log('已完成')
              } else {
                console.log('未完成')
              }
            })
          }
        let id = options.id||options.scene
        let data = '<p style="color:red">123465</p>'
        this.getArticle(id)
    },
    goDetail(e){
        let {id} = e.currentTarget.dataset
        wx.navigateTo({
          url: '/packageA/pages/articleDetail?id='+id,
        })
    },
    getGitAddress(e){
        wx.setClipboardData({
            data: e.currentTarget.dataset.gitaddress,
            success (res) {

            }
          })
          wx.showModal({
            confirmText: '确定',
            content: e.currentTarget.dataset.gitaddress,
            showCancel: false,
            title: '地址已复制请注意保存',
            success: (result) => {},
            fail: (res) => {},
            complete: (res) => {},
          })
          wx.hideLoading({
            success: (res) => {},
          })
    },
    downLoad(e){
        wx.showLoading({
          title: '请稍等...',
          mask:true
        })
        if (videoAd) {
          this.setData({
            e:e
          })
          videoAd.show()
            .then(() => {
              wx.hideLoading({
                success: (res) => {},
              })
            })
            .catch(() => {
              wx.hideLoading({
                success: (res) => {},
              })
              wx.showToast({
                title: '请5s后重试',
                duration:2000
              })
            })
        } else {
          wx.hideLoading({
            success: (res) => {},
          })
        }
      },
    getPermission(e){
        wx.request({
            url: 'https://guanhe.work/xiaochengxu/addPermission',
            data:{
                keyName:e.currentTarget.dataset.keyname
            },
            success(res){
                if(res.data.code == 200){
                    console.log(res)
                }
                wx.setClipboardData({
                    data: `key:${res.data.data.key}\nkeyName:${res.data.data.keyName}`,
                    success (res) {

                    }
                  })
                  wx.showModal({
                    confirmText: '确定',
                    content: `key:${res.data.data.key}\nkeyName:${res.data.data.keyName}`,
                    showCancel: false,
                    title: '内容已复制请注意保存',
                    success: (result) => {},
                    fail: (res) => {},
                    complete: (res) => {},
                  })
                    wx.hideLoading({
                        success: (res) => {},
                      })
                  
            },
            fail(e){
                wx.hideLoading({
                  success: (res) => {},
                })
            }
        })
    },
    getArticleList(){
        let that = this
        wx.request({
            url: 'https://guanhe.work/getArticleList',
            data:{
                type:'api'
            },
            success(res){
                if(res.data.code == 200){
                    let arr = []
                    res.data.data.map(item=>{
                        that.data.article.apilist.map(itemIn=>{
                            if(item.id == itemIn){
                                arr.push(item)
                            }
                        })
                    })
                    that.setData({
                        apiList:arr
                    })
                }
                    wx.hideLoading({
                        success: (res) => {},
                      })
                  
            },
            fail(e){
                wx.hideLoading({
                  success: (res) => {},
                })
            }
        })
    },
    getArticle(id){
        let that = this
        wx.request({
            url: 'https://guanhe.work/getdetail',
            data:{
                id,
                admin:'1'
            },
            success(res){
                if(res.data.code == 200){
                    console.log(res.data.data[0])
                    let style="width:750rpx;overflow:auto;margin:10px 0;padding:10px;background:#333;border-radius:4px;color:white;font-size:18px;"
                    res.data.data[0].content = res.data.data[0].content.replace(/\<\/code\>/g,'</div>').replace(/\<code/g,`<div style="${style}"`)
                    that.setData({
                        article:res.data.data.map(item=>{
                            item.apilist = JSON.parse(item.apilist)
                            return item
                        })[0]
                    })
                    if(that.data.article.articletype=='article'&&that.data.article.apilist.length!=0){
                        that.getArticleList()
                    }
                    // WxParse.wxParse('article', 'html', res.data.data[0].content, that, 5);
                }
                    wx.hideLoading({
                        success: (res) => {},
                      })
                  
            },
            fail(e){
                wx.hideLoading({
                  success: (res) => {},
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})